<?php
namespace Little\Controller;
use  Think\Controller;
//**********************良心产品****************************

class GoodsController extends Controller{
    //商品展示
    public function index(){
        $goods_type_id = I('type'); //商品分类
        //分类
        $list_type = sels("goods_type","id,name","","id asc");
        //默认显示推荐的   status=1   代表商品未下架
        if($goods_type_id){
            $goods = sels("goods","id,banner,name,moneys","status = 1 and goods_type_id = {$goods_type_id}","id desc",6);
        }else{
            $goods = sels("goods","id,banner,name,moneys","status = 1 and goods_type_id = {$list_type[0]['id']}","id desc",6);
        }
        //优惠券
        $coupon = coupons(); $coupons = $coupon['coupons'];  $zong   = $coupon['zong'];  $counts = $coupon['counts'];
        if($goods){$goods = goods_xun($goods);}else{$goods = 1;}
        //轮播图 news_lun status =1 商品 2资讯  3会员卡  4攻略课堂 5线上报名 6题库
        $lun = sels("news_lun", "", "type = 4", "id desc");
        if ($lun) {
            foreach ($lun as $k => $v) {
                $lun[$k]['img'] = IMGS . $v['img'];
            }
        } else {
            $lun = 1;
        }
        echo json_encode(array("goods"=>$goods,"type"=>$list_type,"coupons" => $coupons, "zong" => $zong, "counts" => $counts,"lun"=>$lun));
    }
    //下拉加载
    public function la()
    {
        $currpage      = I('currpage');
        $goods_type_id = I('type'); //商品分类
        //分类
        if(!$goods_type_id){
            $list_type = sels("goods_type","id,name","","id asc");
            $goods_type_id = $list_type[0]['id'];
        }
        $table    = "goods";
        $filed   = "id,banner,name,moneys";
        $where   = "status = 1 and goods_type_id = {$goods_type_id}";
        $orders  = "id desc";
        $pagesize = 6;
        $list = currpage($table,$filed,$where,$pagesize,$currpage,$orders);
        if ($list) {
            $list = goods_xun($list);
        } else {
            $list = 1;
        }
        echo json_encode(array("goods" => $list));
    }
    //评论
    public function pings(){ //外部评论级别切换  以及  查看全部 跳入详情页
        $table    = "comment_goods";
        $goods_id = I('id');
        $type     = I('type');//1好评4-5颗星 2一般2-3颗星  3不满意数1颗星以下   数量
        //商品评价信息
        $ping = ping_info($goods_id);
        //默认显示好评
        if($type == 1 || !$type || $type == "undefined"){
            $comment_goods   = joins('comment_goods',"a.status,a.create_at,a.content,b.headimgurl,b.nickname",array("a left join __USERS__ b on a.user_id = b.id"),"a.goods_id = {$goods_id} and a.xing_one > 3","a.id desc",1);
        }
        if($type == 2){
            $comment_goods   = joins('comment_goods',"a.status,a.create_at,a.content,b.headimgurl,b.nickname",array("a left join __USERS__ b on a.user_id = b.id"),"a.goods_id = {$goods_id} and a.xing_one >1 and a.xing_one < 4","a.id desc",1);
        }
        if($type == 3){
            $comment_goods   = joins('comment_goods',"a.status,a.create_at,a.content,b.headimgurl,b.nickname",array("a left join __USERS__ b on a.user_id = b.id"),"a.goods_id = {$goods_id} and a.xing_one < 2","a.id desc",1);
        }
        if($comment_goods){
            foreach ($comment_goods as $k=>$v){
                $comment_goods[$k]['create_at'] = date("Y-m-d H:i",$v['create_at']);
                if($v['status'] == 2){
                    $comment_goods[$k]['nickname'] = "匿名用户";
                }
            }
        }else{
            $comment_goods   = joins('comment_goods',"a.status,a.create_at,a.content,b.headimgurl,b.nickname",array("a left join __USERS__ b on a.user_id = b.id"),"a.goods_id = {$goods_id}","a.id desc",1);
            if(!$comment_goods){$comment_goods = 1;}else{
                foreach ($comment_goods as $k=>$v){
                    $comment_goods[$k]['create_at'] = date("Y-m-d H:i",$v['create_at']);
                    if($v['status'] == 2){
                        $comment_goods[$k]['nickname'] = "匿名用户";
                    }
                }
            }
        }
        //echo json_encode(array("ping_counts"=>$ping['ping'],"ping_info"=>$comment_goods,"z_p"=>$zs_p,"fu_p"=>$fus_p,"miao_p"=>$miaos_p,"z_ps"=>$z_ps,"fu_ps"=>$fu_ps,"miao_ps"=>$miao_ps));
        echo json_encode(array("ping_counts"=>$ping['ping'],"ping_info"=>$comment_goods));
    }
    public function pingss(){   //评论详情页 内部评论级别切换
        $table    = "comment_goods";
        $goods_id = I('id');
        $type     = I('type');//1好评4-5颗星 2一般2-3颗星  3不满意数1颗星以下   数量
        //描述相符
        $miao = sels($table,"xing_one","goods_id = {$goods_id}");
        if($miao){
            $miao_c = count($miao);
            foreach ($miao as $k=>$v){
                $miao_x[] = $v['xing_one'];
            }
            $miao_z = 0;
            for($i=0;$i<count($miao_x);$i++){
                $miao_z += $miao_x[$i];
            }
            $miao_p = ceil($miao_z/$miao_c);
        }else{
            $miao_p = 1;//暂无
        }
        //服务质量
        $fu = sels($table,"xing_two","goods_id = {$goods_id}");
        if($fu){
            $fu_c = count($fu);
            foreach ($fu as $k=>$v){
                $fu_x[] = $v['xing_two'];
            }
            $fu_z = 0;
            for($i=0;$i<count($fu_x);$i++){
                $fu_z += $fu_x[$i];
            }
            $fu_p = ceil($fu_z/$fu_c);
        }else{
            $fu_p = 1;//暂无
        }
        //综合评价
        $z_p    = ceil(($fu_p+$miao_p)/2);
        $miaos_p = xing($miao_p);    $fus_p    = xing($fu_p);        $zs_p     = xing($z_p);
        $miao_ps = xings($miao_p);   $fu_ps    = xings($fu_p);       $z_ps     = xings($z_p);
        //商品评价信息
        $ping = ping_info($goods_id);
        //默认显示好评
        if($type == 1 || !$type || $type == "undefined"){
            $comment_goods   = joins($table,"a.status,a.create_at,a.content,b.headimgurl,b.nickname",array("a left join __USERS__ b on a.user_id = b.id"),"a.goods_id = {$goods_id} and a.xing_one > 3","a.id desc");
        }
        if($type == 2){
            $comment_goods   = joins($table,"a.status,a.create_at,a.content,b.headimgurl,b.nickname",array("a left join __USERS__ b on a.user_id = b.id"),"a.goods_id = {$goods_id} and a.xing_one >1 and a.xing_one < 4","a.id desc");
        }
        if($type == 3){
            $comment_goods   = joins($table,"a.status,a.create_at,a.content,b.headimgurl,b.nickname",array("a left join __USERS__ b on a.user_id = b.id"),"a.goods_id = {$goods_id} and a.xing_one < 2","a.id desc");
        }
        if($comment_goods){
            foreach ($comment_goods as $k=>$v){
                $comment_goods[$k]['create_at'] = date("Y-m-d H:i",$v['create_at']);
                if($v['status'] == 2){
                    $comment_goods[$k]['nickname'] = "匿名用户";
                }
            }
        }else{$comment_goods = 1;}
        echo json_encode(array("ping_counts"=>$ping['ping'],"ping_info"=>$comment_goods,"z_p"=>$zs_p,"fu_p"=>$fus_p,"miao_p"=>$miaos_p,"z_ps"=>$z_ps,"fu_ps"=>$fu_ps,"miao_ps"=>$miao_ps));
    }
    //领取优惠券
    public function coupons()
    {
        $code = I('code');
        $users = users("users", "id", $code);
        $data['user_id'] = $users['id'];
        //一次性全部领取
        $time = time();
        $coupons = sels("coupons", "id", array("end_at" => array("gt", $time)));
        $pan = 2;//判断用户是否领取过   1没有领取过  2已经领取
        foreach ($coupons as $k => $v) {
            $data['coupons_id'] = $v['id'];
            //判断用户是否领取过
            $test = sel("users_coupons", "id", "user_id = {$users['id']} and coupons_id = {$data['coupons_id']}");
            if (!$test) {
                $data['create_at'] = time();
                $ling = ins("users_coupons", $data);
                $pan = 1;
            }
        }
        if ($pan == 2) {
            echo 1;
        } else {
            if ($ling) {
                echo 2;
            } else {
                echo 3;
            }
        }
    }
    //加入购物车
    public function cars()
    {
        $data['goods_id']  = I('id');  //商品id
        $data['nums']       = I('nums');//数量
        $data['xing']       = I('no');//型号
        $data['moneys']     = I('moneys');//型号
        $data['create_at'] = time();
        $code               = I('code');
        if ($code) {
            $users = users("users", "id,covers", $code);
            $data['user_id'] = $users['id'];
            $res = ins("cars", $data);
            if ($res) {
                echo 1;
            } else {
                echo 2;
            }
        } else {
            echo 2;
        }

    }
}